package cc.siyecao.mapper.wrapper;

import cc.siyecao.mapper.SharedString;
import cc.siyecao.mapper.function.Do;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cc.siyecao.mapper.Criteria;

import java.util.ArrayList;
import java.util.List;

public abstract class Wrapper<W> implements IWrapper<W> {
    /**
     * 占位符
     */
    @JsonIgnore
    public final W typedThis = (W) this;

    /**
     * 去重
     */
    boolean distinct = false;
    /**
     * 条件类型
     */
    SharedString currentType = new SharedString( "normal" );
    /**
     * 多组条件
     */
    List<Criteria> criterias = new ArrayList<>();
    /**
     * 分组条件
     */
    SharedString groupByClause = SharedString.emptyString();
    /**
     * sql结束语句
     */
    SharedString endSql = SharedString.emptyString();
    /**
     * SQL注释
     */
    SharedString sqlComment = SharedString.emptyString();
    /**
     * SQL起始语句
     */
    SharedString startSql = SharedString.emptyString();
    /**
     * 排序语句
     */
    SharedString orderByClause = SharedString.nullString();
    /**
     * 指定更新列
     */
    Criteria setCriteria = new Criteria( "set" );
    /**
     * 指定查询列
     */
    SharedString sqlSelect = SharedString.emptyString();

    @Override
    public W maybeDo(boolean condition, Do something) {
        if (condition) {
            something.doIt();
        }
        return typedThis;
    }

    /**
     * 条件清空
     */
    abstract void clear();

    public String getCurrentType() {
        return currentType.getStringValue();
    }

    public List<Criteria> getCriterias() {
        return criterias;
    }

    public String getGroupByClause() {
        return groupByClause.getStringValue();
    }

    public String getEndSql() {
        return endSql.getStringValue();
    }

    public String getSqlComment() {
        return sqlComment.getStringValue();
    }

    public String getStartSql() {
        return startSql.getStringValue();
    }

    public String getOrderByClause() {
        return orderByClause.getStringValue();
    }

    public Criteria getSetCriteria() {
        return setCriteria;
    }

    public String getSqlSelect() {
        return sqlSelect.getStringValue();
    }

}
